<?php 
/**
 * 统计控制器
 * @Author    紫梦
 * @DateTime  2018-01-26
 * @copyright 响响租车
 * @version   1.0
 */
namespace app\index\controller;
use app\common\model\Agency;
use app\common\model\Cars;
use app\common\model\Brand;
use think\Db;
use app\common\model\Repair;
use app\common\model\CarsInfo;

class Charts extends Base
{
    public function cars()
    {

        $brand = Brand::column('id');

        $model = Brand::withCount(['cars'])->select($brand);
        foreach ($model as $key => $value) {
            $array[$key]['value'] = $value->cars_count;
            
            $array[$key]['name'] = $value->name;
        }
        rsort($model);
        $name = array_column($array,'name');
        $count = Cars::count();
        $this->assign('brand',json_encode($array)); 
        $this->assign('name',json_encode($name));   
        $this->assign('count',$count);  
        return $this->fetch();
    }
    
    public static function relevance()
    {
        $agency = Agency::where('id','>',1)->column('id');

        $type = 0;
        $array = [];
        $j = 0;
        for ($i=0; $i < 9; $i++) { 
            $model = Agency::withCount(['cars'=>function($query){
                                            global $type;$type++;
                                            $query->where('car_usage',$type);
                                        }])->select($agency);
            foreach ($model as $key => $value) {
                $array[0][$key]['value'][$i] = $value->cars_count;
                $array[0][$key]['name'] = $value->name;
                $array[1][0][$i][$key] = $value->cars_count;
                $array[2]['name'][$key] = $value->name;
                $j += $value->cars_count;
            }
        }
        $array['total'] = $j;
        return $array;
    }

    public static function abnormal(){
        $data = Db::name('cars_info')->where(['network_status'=>0])->where('machine_report_time','>',0)->order('machine_report_time asc')->column('car_unique_id');
        $cars = [];
        $x = 0;
        while ( count($cars)<=10 && $x<count($data)) {
           $dataum = Cars::with('brand,agency,carsInfo')->where(['car_unique_id'=>$data[$x]])->find();
           $x++;
            if($dataum){
                $cars[] = $dataum;
            }
           
        }
        if($cars){
            foreach ($cars as $key => $value) {
                $time = time()*1000 - ($value['cars_info']['machine_report_time']);
                $cars[$key]['dataum'] = (floor($time/1000/86400));
            }

        }else{
            $cars = [];
        }
        
      return $cars;
    }

    public static function repair(){
        $data = Db::name('repair')->where('shift_time','eq',0)->order('in_time asc')->limit(5)->select();
        return $data;
    }

    public static function business(){

        $agency = Agency::where('id','>',1)->column('id');
        foreach ($agency as $key => $value) {
           $data[] = Cars::where(['agency_id'=>$value])->where('delete_time',null)->count();
        }
        return $data;
    }

    public static function usage(){
        $type = 0;
        for ($i=0; $i < 9; $i++) { 
            $type++;
            $array[$i] = Db::name('cars')->where(['car_usage'=>$type])->where('delete_time',null)->count();
        }
        return $array;
    }

    public static function agency(){
        $agency_id = Db::name('agency')->column('id');
        for ($i=0; $i < count($agency_id); $i++) { 
            $array[$i] = Db::name('cars')->where(['agency_id'=>$agency_id[$i]])->where('delete_time',null)->count();
        }
        return $array;
    }
}
